#!/usr/local/bin/lua

--local a = "aaaaaa,3,1,2;aaaaaa,3,3,4;5,6;"
local a = "1 21 31 41 51 6 7"

local split = function (str)
    if type(str) ~= "string" then
        return;
    end

    local strTable = {}

    for txt in string.gmatch(str, '(%s*%d+%s*)') do
    --for txt in string.gmatch(str, '(%d+%s*,%s*%d+%s*;)') do
        strTable[#strTable + 1] = txt
    end

    return strTable
end

local strTable = split(a)
for idx,val in ipairs(strTable) do
    print(idx, val)
end
